package es.techideas.appsjam;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class GreenPointsMapActivity extends MapActivity{

	private static Context context;
	
	private MapView mapView;
	
	private ProgressDialog pd;

	private Button back;
	
	public static MyLocationOverlay myLocationOverlay;
	
	private GenericItemizedOverlay greenPointsBOverlay;
	
	private GenericItemizedOverlayM greenPointsMOverlay;
	
	private Handler h = new Handler ();
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.greenpointsmap);
        context = getApplicationContext();
        
        initializeView();
        

        myLocation();
    }
	
	private void initializeView(){
		
		back = (Button) findViewById(R.id.greenpoints_back);
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.getController().setZoom(18);
		
		// go to MainActivity Button
		back.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	private void myLocation() {
		
		myLocationOverlay = new MyLocationOverlay(this, mapView);
		
		pd = new ProgressDialog(this);
		pd.setMessage("Buscando su posici�n...");
		pd.show();
		pd.setCanceledOnTouchOutside(true);
		pd.setButton("ok", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				pd.dismiss();
			}
		});
		
		myLocationOverlay.enableMyLocation();
		
		myLocationOverlay.runOnFirstFix(new Runnable() {
			
			public void run() {
				pd.cancel();
				mapView.getController().animateTo(myLocationOverlay.getMyLocation());
			}
		});
		
		
	
		//myLocationOverlay.enableCompass();
		mapView.getOverlays().add(myLocationOverlay);
	
		//zoomControls
			MapView.LayoutParams params = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, mapView.getWidth(), mapView.getHeight(),MapView.LayoutParams.RIGHT);
			mapView.addView(mapView.getZoomControls(), params);
					
			
			mapView.invalidate();
			
			
			Drawable markerB=getResources().getDrawable(R.drawable.greenpointb);
			int markerWidthB = markerB.getIntrinsicWidth();
			int markerHeightB = markerB.getIntrinsicHeight();
			markerB.setBounds(0, markerHeightB, markerWidthB, 0);
			
			// create the B points overlay
			greenPointsBOverlay = new GenericItemizedOverlay(markerB,getParent());
			mapView.getOverlays().add(greenPointsBOverlay);
			
			Drawable markerM=getResources().getDrawable(R.drawable.greenpointm);
			int markerWidthM = markerM.getIntrinsicWidth();
			int markerHeightM = markerM.getIntrinsicHeight();
			markerM.setBounds(0, markerHeightM, markerWidthM, 0);
			
			// create the M points overlay
			greenPointsMOverlay = new GenericItemizedOverlayM(markerM,getParent());
			mapView.getOverlays().add(greenPointsMOverlay);
			
			//mapView.setOnTouchListener(this);
			goToMyLocation();
			drawMapPoints();
	}
	
	private void goToMyLocation() {
		h.post(new Runnable(){
    		public void run() {
    			GeoPoint gp = null;
				while (gp == null){
					gp = mapView.getMapCenter();
					if (gp != null) {
						mapView.getController().animateTo(gp);
					
					}
					else {
						Toast toast = Toast.makeText(getApplicationContext(), "No se ha encontrado posici�n" ,Toast.LENGTH_SHORT);
						toast.show();
						System.out.println("gp is null");
					}
				}
				
				
			}
			
		});
	}
	
	public static void onTap(){
		Toast t = Toast.makeText(context,"Horari: Dill - Diss de 8 a 14 i 16 a 19:30", Toast.LENGTH_LONG);
		t.show();
		
	}
	
	public static void onTapM(String text){
		Toast t = Toast.makeText(context, text , Toast.LENGTH_LONG);
		t.show();
	}
	
	private void drawMapPoints(){
		
		// hardcoded
		// Clot
		GeoPoint p1 = new GeoPoint(41408231,2189755);
		greenPointsBOverlay.addItem(p1, "Punt", "Punt Verd");
		
		// Rambla del PobleNou
		GeoPoint p2 = new GeoPoint(41394936,2182374);
		greenPointsMOverlay.addItem(p2, "Punt", "Dimarts de 16:00 a 19:30h");
		
		// Espronceda
		GeoPoint p3 = new GeoPoint(41404111,2197309);
		greenPointsMOverlay.addItem(p3, "Punt", "Divendres de 16:00 a 19:30h");
		
		// Marina
		GeoPoint p4 = new GeoPoint(41410049,2198403);
		greenPointsMOverlay.addItem(p4, "Punt", "Dijous de 16:00 a 19:30h");
		
		// FortPienc
		GeoPoint p5 = new GeoPoint(41395049,2186537);
		greenPointsBOverlay.addItem(p5, "Punt", "Punt Verd");
		
		
	}
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
